package com.happystreet.config
{
	import com.adobe.serialization.json.JSON;
	import com.happystreet.engine.CoreShop;
	import com.happystreet.info.FamilyInfo;
	import com.happystreet.info.GoodInfo;
	import com.happystreet.ui.CustomersPopup;
	
	import flash.utils.Timer;
	
	public class CustomersConfig 
	{
		
		public static const GOOD_LOCKED:String = "GoodLocked";
		public static const GOOD_EMPTY:String = "GoodEmpty";
		public static const GOOD_AVAILABLE:String = "GoodAvailable";
		public static const GOOD_UNAVAILABLE:String = "GoodUnavailable";
		public static const GOOD_SHOP_CLOSED:String = "GoodShopClosed";
		public static const GOOD_SHOP_UGLY:String = "GoodShopUgly";
		private static const DEFAULT_DELIGHTED_STEPS:Array = [30, 35, 40, 45, 100];
		private static const SATISFACTION_MULTIPLIER:Array = [1, 2, 4, 4];
		public static const CUSTOMER_MAX_LEVEL:int = 3;
		public static const CUSTOMER_COUNT_PER_FAMILY:int = 10;
		
		private static var multiLoveUnlockLevel:uint;
		private static var multiLove3UnlockLevel:uint;
		private static var multiLove4UnlockLevel:uint;
		private static var multiLove5UnlockLevel:uint;
		private static var familyInfoTable:Array;
		private static var customersConfigList:Object;
		public static var focusedCustomer:Object;
		public static var nbFamilies:int;
		public static var oldCustomer_data:Object;
		private static var goodsStatus:Object;
		public static var goodsRates:Object;
		private static var currentDelightHour:int;
		private static var nextDelightTimer:Timer;
		public static var newCustomers:Object;
		
		
		public static function levelUpCustomer(_arg1:int):void
		{
			if (GameConfig.userInfo.customer_data.hasOwnProperty(String(_arg1)))
			{
				GameConfig.userInfo.customer_data[String(_arg1)].level = (int(GameConfig.userInfo.customer_data[String(_arg1)].level) + 1);
			}
		}
		
		public static function getGoodStatus(_arg1:int, _arg2:int):String
		{
			var _local5:int;
			var _local6:Object;
			var _local3:int = GoodsConfig.getTargetShopTypeByGoodId(_arg1);
			var _local4:int = GameConfig.getShopIdByType(_local3);
			if (_local4 < 0)
			{
				goodsStatus[String(_arg1)] = GOOD_UNAVAILABLE;
			}
			else
			{
				_local5 = GameConfig.shopList[_local4].shopPosition;
				if (GameConfig.shopList[_local5].buildStatus != CoreShop.WORKING_SHOP_STATUS)
				{
					goodsStatus[String(_arg1)] = GOOD_SHOP_CLOSED;
				}
				else
				{
					if (GameConfig.shopList[_local5].beautyLevel < _arg2)
					{
						goodsStatus[String(_arg1)] = GOOD_SHOP_UGLY;
					}
					else
					{
						_local6 = GameConfig.shopData[_local5].goods;
						if (!_local6.hasOwnProperty(_arg1))
						{
							goodsStatus[String(_arg1)] = GOOD_LOCKED;
						}
						else
						{
							if (_local6[String(_arg1)] <= 0)
							{
								goodsStatus[String(_arg1)] = GOOD_EMPTY;
							}
							else
							{
								goodsStatus[String(_arg1)] = GOOD_AVAILABLE;
							}
						}
					}
				}
			}
			return (goodsStatus[String(_arg1)]);
		}
		
		public static function addNewCustomerData(_arg1:int):void
		{
			var _local2:Object;
			if (!GameConfig.userInfo.customer_data.hasOwnProperty(String(_arg1)))
			{
				_local2 = {
					"sat":0,
					"level":0
				}
				GameConfig.userInfo.customer_data[String(_arg1)] = _local2;
			}
			newCustomers[String(_arg1)] = true;
			CustomersPopup.needsNewCustomerBubble = true;
			focusedCustomer.customerId = _arg1;
			focusedCustomer.customerFamily = getCustomerConfig(_arg1).family;
			focusedCustomer.familyIndex = getFamilyPositionById(focusedCustomer.customerFamily);
			focusedCustomer.customerIndex = 0;
		}
		
		public static function getCustomerFamilyPositionById(_arg1:int):int
		{
			var _local2:Object = getCustomerConfig(_arg1);
			return (getFamilyPositionById(_local2.family));
		}
		
		private static function createCustomersListFromXML(_arg1:XML):void
		{
			var _local5:XML;
			var _local6:XMLList;
			var _local7:XML;
			var _local8:Object;
			var _local9:Array;
			var _local10:Array;
			var _local11:FamilyInfo;
			customersConfigList = new Object();
			var _local2:XMLList = _arg1..customer;
			var _local3:XMLList = _arg1..params;
			var _local4:Array = JSON.decode(_local3[0].@levelsForNewMultiLove);
			multiLoveUnlockLevel = _local4[1];
			multiLove3UnlockLevel = _local4[2];
			multiLove4UnlockLevel = _local4[3];
			multiLove5UnlockLevel = _local4[4];
			for each (_local5 in _local2)
			{
				_local8 = new Object();
				_local8.id = int(_local5.@id);
				_local8.symbol = _local5.@symbol.toXMLString();
				_local8.scale = ((_local5.@scale) ? int(_local5.@scale) : 100);
				_local8.name = _local5.@name.toXMLString();
				_local8.family = int(_local5.parent().@id);
				_local8.speed = int(_local5.@speed);
				_local8.kindness = int(_local5.@kindness);
				_local8.beauty = int(_local5.@beauty);
				_local8.maxSatisfaction = int(_local5.@maxSatisfaction);
				_local8.mandatory = (((_local5.@mandatoryCustomers.toXMLString())!="") ? JSON.decode(_local5.@mandatoryCustomers.toXMLString()) : null);
				_local8.deltaPerLevel = JSON.decode(_local5.@deltaPerLevel.toXMLString());
				_local9 = JSON.decode(_local5.@goods.toXMLString());
				_local8.goods_full_list = _local9;
				_local10 = JSON.decode(_local5.@goodsOdds.toXMLString());
				_local8.goods_odds_full_list = _local10;
				_local8.goodsDeltas = JSON.decode(_local5.@deltaPerGood.toXMLString());
				customersConfigList[_local5.@id] = _local8;
			}
			_local6 = _arg1..family;
			familyInfoTable = new Array();
			for each (_local7 in _local6)
			{
				_local11 = new FamilyInfo(_local7);
				familyInfoTable[(_local7.@position - 1)] = _local11;
			}
		}
		
		public static function checkFamilyIsAvailable(_arg1:int):Boolean
		{
			var _local2:int = getCustomerIdByFamilyAndIndex(_arg1, 1);
			return (GameConfig.userInfo.customer_data.hasOwnProperty(String(_local2)));
		}
		
		public static function getFamilyPositionById(_arg1:int):int
		{
			var _local3:FamilyInfo;
			var _local2:int = 1;
			for each (_local3 in familyInfoTable)
			{
				if (_local3.id == _arg1)
				{
					return (_local2);
				}
				_local2++;
			}
			return -1;
		}
		
		public static function get MULTILOVE4_UNLOCK_LEVEL():uint
		{
			return (multiLove4UnlockLevel);
		}
		
		public static function isLastMemberOfTheFamily(_arg1:int):Boolean
		{
			var _local2:int = getCustomerConfig(_arg1).family;
			return (((_arg1 - (_local2 * 100)) == CUSTOMER_COUNT_PER_FAMILY));
		}
		
		public static function getCustomerIdByFamilyAndIndex(_arg1:int, _arg2:int):int
		{
			var _local3:int = familyInfoTable[(_arg1 - 1)].id;
			return (((_local3 * 100) + (_arg2 + 1)));
		}
		
		public static function getCustomersForRush(_arg1:int):Object
		{
			var _local5:Array;
			var _local6:int;
			var _local7:String;
			var _local8:int;
			var _local9:String;
			var _local10:GoodInfo;
			var _local11:Object;
			var _local12:Array;
			var _local13:Boolean;
			var _local14:int;
			var _local15:int;
			var _local16:Boolean;
			var _local17:Boolean;
			var _local18:int;
			var _local19:int;
			var _local20:int;
			var _local2:Boolean;
			var _local3:Boolean;
			var _local4:Array = new Array();
			if (_local3)
			{
				for (_local9 in GameConfig.shopData[_arg1].goods)
				{
					if (GameConfig.shopData[_arg1].goods[_local9] > 0)
					{
						_local4.push(int(_local9));
					}
				}
			}
			else
			{
				for each (_local10 in GoodsConfig.getGoodsListByShopType(GameConfig.shopList[_arg1].shopLayout.type))
				{
					_local4.push(_local10.goodId);
				}
			}
			_local5 = new Array();
			for (_local7 in GameConfig.userInfo.customer_data)
			{
				_local11 = getCustomerConfig(int(_local7));
				_local12 = _local11.goods;
				_local13 = (((_local11.beauty)>GameConfig.shopList[_arg1].beautyLevel) ? false : true);
				for each (_local14 in _local12)
				{
					for each (_local15 in _local4)
					{
						_local16 = false;
						if (((GameConfig.shopData[_arg1].goods.hasOwnProperty(String(_local15))) && ((GameConfig.shopData[_arg1].goods[String(_local15)] > 0))))
						{
							_local16 = true;
						}
						if (_local15 == _local14)
						{
							if (_local5.indexOf(int(_local7)) < 0)
							{
								_local17 = true;
								if (_local2)
								{
									_local18 = GameConfig.userInfo.customer_data[_local7].sat;
									_local19 = _local11.maxSatisfaction;
									_local20 = (_local19 * CustomersConfig.getSatisfactionMultiplier(int(_local7)));
									if (_local18 >= _local20)
									{
										_local17 = false;
									}
								}
								if (((_local16) && (_local13)))
								{
									_local6++;
								}
								if (_local17)
								{
									_local5.push(int(_local7));
								}
							}
						}
					}
				}
			}
			_local8 = Math.ceil(((_local6 / _local5.length) * 100));
			return ({
				"customersList":_local5,
				"happyCustomersPercentage":_local8
			})
		}
		
		public static function get MULTILOVE3_UNLOCK_LEVEL():uint
		{
			return (multiLove3UnlockLevel);
		}
		
		public static function getFamilyInfo(_arg1:int):FamilyInfo
		{
			return (familyInfoTable[(_arg1 - 1)]);
		}
		
		public static function checkCustomerIsNew(_arg1:int):Boolean
		{
			var _local3:String;
			var _local2:String = String(_arg1);
			for (_local3 in newCustomers)
			{
				if (_local2 == _local3)
				{
					return (true);
				}
			}
			return false;
		}
		
		public static function getCustomerConfig(_arg1:int):Object
		{
			var _local6:int;
			var _local7:int;
			var _local2:Object = customersConfigList[_arg1];
			var _local3:int = ((GameConfig.userInfo.customer_data.hasOwnProperty(String(_arg1))) ? GameConfig.userInfo.customer_data[String(_arg1)].level : 0);
			var _local4:int = Math.min(_local2.goods_full_list.length, (_local3 + 1));
			_local2.goods = _local2.goods_full_list.slice(0, _local4);
			_local2.goodsOdds = new Array();
			var _local5:int;
			while (_local5 < _local4)
			{
				_local6 = 0;
				_local7 = 0;
				while (_local7 <= _local5)
				{
					_local6 = (_local6 + _local2.goods_odds_full_list[(_local4 - 1)][_local7]);
					_local7++;
				}
				_local2.goodsOdds.push(_local6);
				_local5++;
			}
			return (_local2);
		}
		
		public static function get MULTILOVE5_UNLOCK_LEVEL():uint
		{
			return (multiLove5UnlockLevel);
		}
		
		public static function getCustomerIdsforFamilies(_arg1:Array):Array
		{
			var _local3:int;
			var _local4:Object;
			var _local2:Array = new Array();
			for each (_local3 in _arg1)
			{
				for each (_local4 in customersConfigList)
				{
					if (_local4.family == _local3)
					{
						_local2.push(_local4.id);
					}
				}
			}
			return (_local2);
		}
		
		public static function setCustomersConfig(_arg1:String):void
		{
			var _local2:XML = new XML(_arg1);
			createCustomersListFromXML(_local2);
			focusedCustomer = new Object();
			focusedCustomer.customerId = 101;
			focusedCustomer.customerFamily = 1;
			focusedCustomer.familyIndex = 0;
			newCustomers = new Object();
			goodsStatus = new Object();
			goodsRates = new Object();
		}
		
		public static function cleanNewCustomersList():void
		{
			var _local1:String;
			for (_local1 in newCustomers)
			{
				delete newCustomers[_local1];
			}
		}
		
		public static function getCustomerIndexById(_arg1:int):int
		{
			var _local2:int;
			while (_local2 < GameConfig.userInfo.customer_data.length)
			{
				if (GameConfig.userInfo.customer_data[_local2][0] == _arg1)
				{
					return (_local2);
				}
				_local2++;
			}
			return -1;
		}
		
		public static function getNbFamilies():int
		{
			return (familyInfoTable.length);
		}
		
		public static function checkCustomerAvailableByMandatories(_arg1:int):Boolean
		{
			var _local4:int;
			var _local5:int;
			var _local2:Object = getCustomerConfig(_arg1);
			var _local3:Boolean = true;
			for each (_local4 in _local2.mandatory)
			{
				_local5 = CustomersConfig.getCustomerIndexById(_local4);
				if (_local5 != -1)
				{
					if (GameConfig.userInfo.customer_data[_local5][1] < getCustomerConfig(_local4).maxSatisfaction)
					{
						_local3 = false;
					}
				}
				else
				{
					_local3 = false;
				}
			}
			return (_local3);
		}
		
		public static function getFamilyTreeLayout():Array
		{
			var _local1:Array = [[{"parents":null}, {"parents":null}, {"parents":null}, {"parents":null}], [{"parents":[0, 1]}, {"parents":[1, 2]}, {"parents":[2, 3]}], [{"parents":[4, 5]}, {"parents":[5, 6]}], [{"parents":[7, 8]}]];
			return (_local1);
		}
		
		public static function getSatisfactionMultiplier(_arg1:int, _arg2:int=0):int
		{
			var _local3:int = GameConfig.userInfo.customer_data[String(_arg1)].level;
			var _local4:int = CustomersConfig.getCustomerFamilyPositionById(_arg1);
			var _local5:int = ((GameConfig.userInfo.achievements.hasOwnProperty("family")) ? GameConfig.userInfo.achievements.family[_local4] : 0);
			var _local6:int = Math.min(_local3, _local5);
			var _local7:int = (SATISFACTION_MULTIPLIER.length - 1);
			return (SATISFACTION_MULTIPLIER[Math.min(_local6, _local7)]);
		}
		
		public static function createGoodsRates(_arg1:Array):void
		{
			var _local2:String;
			var _local3:Object;
			var _local4:int;
			var _local5:int;
			var _local6:int;
			var _local7:String;
			var _local8:Number;
			for (_local2 in goodsRates)
			{
				delete goodsRates[_local2];
			}
			for each (_local3 in _arg1)
			{
				_local4 = 0;
				while (_local4 < _local3.goods.length)
				{
					_local5 = GoodsConfig.getTargetShopTypeByGoodId(_local3.goods[_local4]);
					_local6 = GameConfig.getShopIdByType(_local5);
					if (_local6 >= 0)
					{
						if (_local3.beauty <= GameConfig.shopList[_local6].beautyLevel)
						{
							_local7 = String(_local3.goods[_local4]);
							_local8 = (1 / _local3.goodsDeltas[_local4]);
							if (goodsRates.hasOwnProperty(_local7))
							{
								goodsRates[_local7] = (goodsRates[_local7] + _local8);
							}
							else
							{
								goodsRates[_local7] = _local8;
							}
						}
					}
					_local4++;
				}
			}
		}
		
		public static function increaseSatisfaction(_arg1:int, _arg2:uint, _arg3:int):Boolean
		{
			GameConfig.userInfo.customer_data[String(_arg1)].sat = (int(GameConfig.userInfo.customer_data[String(_arg1)].sat) + _arg2);
			if (GameConfig.userInfo.customer_data[String(_arg1)].sat < _arg3)
			{
				return (false);
			}
			GameConfig.userInfo.customer_data[String(_arg1)].sat = _arg3;
			return (true);
		}
		
		public static function get MULTILOVE_UNLOCK_LEVEL():uint
		{
			return (multiLoveUnlockLevel);
		}
		
		
	}
}
